package net.sashag.nadlandroid;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.util.Log;
import android.widget.Toast;

import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.RootContext;
import com.googlecode.androidannotations.annotations.UiThread;
import com.parse.ParseException;
import com.parse.ParseUser;

@EBean
public class ListingManagementDialog {

	@RootContext
	protected Context context;
	
	private ProgressDialog progress;
	
	@UiThread
	protected void initProgress() {
		progress = new ProgressDialog(context);
		progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progress.setTitle("Please Wait");
		progress.setMessage("Retrieving your apartment listings from our Web service...");
		progress.setCancelable(false);
		progress.show();
	}
	
	@Background
	public void show() {
		initProgress();
		try {
			Apartment[] apartments = Apartment.getOwnedApartments(ParseUser.getCurrentUser().getObjectId());
			if (apartments.length == 0) {
				showError("You have no apartment listings; perhaps you should submit one?");
			} else {
				showApartments(apartments);
			}
		} catch (ParseException e) {
			showError("Error retrieving your apartments: " + e.getMessage());
		}
	}
	
	@UiThread
	protected void showError(String error) {
		progress.dismiss();
		Toast.makeText(context, error, Toast.LENGTH_LONG).show();
	}
	
	@SuppressLint("UseSparseArrays")
	@UiThread
	protected void showApartments(final Apartment[] apartments) {
		progress.dismiss();
		
		final String[] apartmentNames = new String[apartments.length];
		final boolean[] checkedApartments = new boolean[apartments.length];
		for (int i = 0; i < apartments.length; ++i) {
			apartmentNames[i] = apartments[i].getAddress();
			checkedApartments[i] = apartments[i].isPublished();
		}
		
		final Map<Integer, Boolean> changedApartments = new HashMap<Integer, Boolean>();
		new AlertDialog.Builder(context)
			.setTitle("Publish/Unpublish Apartments")
			.setCancelable(true)
			.setMultiChoiceItems(apartmentNames, checkedApartments, new OnMultiChoiceClickListener() {
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					Log.v("ListingManagementDialog", "Apartment " + apartmentNames[which] + ", checked = " + isChecked);
					changedApartments.put(which, isChecked);
				}
			})
			.setPositiveButton("Submit", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					for (Integer changedIndex : changedApartments.keySet()) {
						Apartment apartment = apartments[changedIndex];
						apartment.updatePublishStateAndSaveInBackground(changedApartments.get(changedIndex));
					}
				}
			})
			.setNegativeButton("Cancel", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {}
			})
			.show();
	}
	
}
